﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ERMap;

namespace PhoneBook.Manage
{
    public class Manage
    {
        public static DataSet getMenu(string userid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select sm.*,smr.rightflag");
            sb.Append(" from sysmodule sm inner join sysmodulerole smr on sm.moduleid = smr.moduleid");
            sb.Append(" inner join sysUserRole sur on sur.roleid = smr.roleid");
            sb.Append(" inner join sysRole sr on smr.roleid = sr.roleid");
            sb.Append(" where sur.userid = @userid");
            sb.Append(" union all");
            sb.Append(" select distinct p_sm.*,1 as rightflag");
            sb.Append(" from sysmodule p_sm inner join ");
            sb.Append(" (select sm.parentid");
            sb.Append(" from sysmodule sm inner join sysmodulerole smr on sm.moduleid = smr.moduleid");
            sb.Append(" inner join sysUserRole sur on sur.roleid = smr.roleid");
            sb.Append(" inner join sysRole sr on smr.roleid = sr.roleid");
            sb.Append(" where sur.userid = @userid) p_sub");
            sb.Append(" on p_sm.moduleid = p_sub.parentid");

            Hashtable param = new Hashtable();
            param["userid"] = userid;

            return ERMapHelper.getCustomData(sb.ToString(), param);
        }

        public static Hashtable GetSysModulesByUrl(string path)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select * ");
            sb.Append(" from sysModule");
            sb.Append(" where urladdress = @path");

            Hashtable param = new Hashtable();
            param["path"] = path;

            Hashtable result = ERMapHelper.getCustomModel(sb.ToString(), param);

            return result;
        }

        public static int getUserModuleRight(string userid, string moduleid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select max(smr.rightflag) as rightflag");
            sb.Append(" from sysModuleRole smr inner join sysUserRole sur on smr.roleid = sur.roleid");
            sb.Append(" where sur.userid = @userid and smr.moduleid = @moduleid");

            Hashtable param = new Hashtable();
            param["userid"] = userid;
            param["moduleid"] = moduleid;

            Hashtable result = ERMapHelper.getCustomModel(sb.ToString(), param);

            int i_result = 0;
            if (result != null)
            {
                int.TryParse(Commons.StrUtil.FormatValue(result["rightflag"]), out i_result);
            }
            return i_result;
        }
    }
}
